package com.weixing.mall.provider.util;

import cn.hutool.core.net.URLEncoder;
import com.alibaba.fastjson.JSON;
import com.weixing.mall.pay.channel.wx.WxPaySet;
import com.weixing.mall.provider.api.model.vo.PaySetVo;
import lombok.extern.slf4j.Slf4j;
import org.weixin4j.Weixin;
import org.weixin4j.WeixinException;
import org.weixin4j.component.MenuComponent;
import org.weixin4j.component.SnsComponent;
import org.weixin4j.model.menu.SingleButton;
import org.weixin4j.model.menu.ViewButton;
import org.weixin4j.model.sns.SnsAccessToken;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WechatUtil
 * @Description
 * @Author Small
 * @Date 2020/2/14 16:40
 * @Version 1.0
 **/
@Slf4j
public class WechatUtil {

    /**
     * 方法描述: 设置微信公众号菜单
     * @Author Small
     * @param
     * @Date 2020/2/14 16:55
     * @return void
    */
    public static void setMenu() {
        Weixin weixin = new Weixin();
        //3.1获取菜单组件
        MenuComponent menuComponet = weixin.menu();
        //调用组件创建方法
        //menuComponet.create(menu);
        //组件创建需要一个Menu对象，我们也可以看到这个对象在org.weixin4j.model目录下
        //model目录下是我们所有开发中用到的对象，也是以组件分的包名
        //例如自定义菜单组件，就是menu，所有相关对象则=在org.weixin4j.model.menu下
        org.weixin4j.model.menu.Menu menu = new org.weixin4j.model.menu.Menu();
        //关于menu对象，有2个构造函数，一个带参，一个不带参
        //带参的构造函数是一个json对象，需要的格式为{menu:{...}}
        //这个menu是必须包含的对象，对象内是一个按钮对象集合，以官方网站示例为例：
        //接下面我们演示用对象的方式创建自定义菜单
        List<SingleButton> buttons = new ArrayList<SingleButton>();

        //#创建菜单1 包含二级子菜单
        SingleButton button1 = new SingleButton("官方商城");
        //##创建二级子菜单1
//        https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac5f1048659280dc&redirect_uri=https%3A%2F%2F1511w8t629.51mypc.cn%2Fuaa%2Fmember%2Flogin%2Fopenid&response_type=code&scope=snsapi_base&state=1#wechat_redirect
        String redirectUrl = "https://1511w8t629.51mypc.cn/uaa/member/login/openid&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        String url = URLEncoder.createDefault().encode(redirectUrl, Charset.defaultCharset());
        url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+weixin.getAppId()+"&redirect_uri=" + redirectUrl;
        ViewButton button1_1 = new ViewButton("时尚系列", url);
        button1.getSubButton().add(button1_1);
        //##创建二级子菜单2
        ViewButton button1_2 = new ViewButton("专柜系列", "http://www.baidu.com/");
        button1.getSubButton().add(button1_2);
        //###添加一级菜单1
        buttons.add(button1);

        //创建菜单2
        SingleButton button2 = new SingleButton("时尚领秀");

        //创建2级子菜单1，打开页面
        ViewButton button2_1 = new ViewButton("年度大秀", "http://www.baidu.com/");
        button2.getSubButton().add(button2_1);
        //创建2级子菜单2，打开页面
        ViewButton button2_2 = new ViewButton("时尚大片", "http://www.baidu.com/");
        button2.getSubButton().add(button2_2);
        //创建2级子菜单2，打开页面
        ViewButton button2_3 = new ViewButton("时尚映像", "http://www.baidu.com/");
        button2.getSubButton().add(button2_3);
        //添加一级菜单2
        buttons.add(button2);

        //创建菜单3
        SingleButton button3 = new SingleButton("品牌发展");

        //创建2级子菜单1，打开页面
        ViewButton button3_1 = new ViewButton("品牌介绍", "http://www.baidu.com/");
        button3.getSubButton().add(button3_1);
        //创建2级子菜单2，打开页面
        ViewButton button3_2 = new ViewButton("品牌动态", "http://www.baidu.com/");
        button3.getSubButton().add(button3_2);
        //创建2级子菜单2，打开页面
        ViewButton button3_3 = new ViewButton("实体专柜", "http://www.baidu.com/");
        button3.getSubButton().add(button3_3);
        //创建2级子菜单2，打开页面
        ViewButton button3_4 = new ViewButton("联系我们", "http://www.baidu.com/");
        button3.getSubButton().add(button3_4);
        //添加一级菜单3
        buttons.add(button3);

        //设置自定义菜单
        menu.setButton(buttons);
        System.out.println(menu.toJSONObject().toJSONString());
        try {

            //调用微信自定义菜单组件，创建自定义菜单
            menuComponet.create(menu);
        } catch (WeixinException ex) {
            //打印创建异常日志
            ex.printStackTrace();
        }
    }

    /**
     * 方法描述: code换取access_token
     * @Author Small
     * @param code
     * @Date 2020/4/3 14:16
     * @return org.weixin4j.model.sns.SnsAccessToken
    */
    public static SnsAccessToken getSnsOAuth2AccessToken(String code){
        SnsComponent snsComponent = new SnsComponent(new Weixin());
        SnsAccessToken snsOAuth2AccessToken = null;
        try {
            snsOAuth2AccessToken = snsComponent.getSnsOAuth2AccessToken(code);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
        return snsOAuth2AccessToken;
    }


    /**
     * 方法描述: code换取open_id
     * @Author Small
     * @param code
     * @Date 2020/4/3 14:18
     * @return java.lang.String
    */
    public static String getOpenIdByCode(String code){
        String openId = null;
        SnsComponent snsComponent = new SnsComponent(new Weixin());
        try {
            openId = snsComponent.getOpenId(code);
        } catch (WeixinException e) {
            log.error("code换取open_id时发生异常：",e);
        }
        return openId;
    }

  /**
     * 方法描述: code换取open_id
     * @Author Small
     * @param code
     * @Date 2020/4/3 14:18
     * @return java.lang.String
    */
    public static String getOpenIdByCode(String code, PaySetVo paySetVo){
        String openId = null;
        String paramSet = paySetVo.getParamSet();
        WxPaySet wxPaySet = JSON.parseObject(paramSet, WxPaySet.class);
        SnsComponent snsComponent = new SnsComponent(new Weixin(wxPaySet.getAppid(),wxPaySet.getSecret()));
        try {
            openId = snsComponent.getOpenId(code);
        } catch (WeixinException e) {
            log.error("code换取open_id时发生异常：",e);
        }
        return openId;
    }



    public static void main(String[] args) throws UnsupportedEncodingException {
//        setMenu();


        String redirectUrl = "https://1511w8t629.51mypc.cn/uaa/member/login/openid";
        String encode = java.net.URLEncoder.encode(redirectUrl, "utf-8");
        System.out.println(encode);
       String  url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac5f1048659280dc&redirect_uri=" + encode + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        System.out.println(url);
    }
}
